package com.musiklegal;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MerchantAdapter extends BaseAdapter
{
	 
	 
	private LayoutInflater mInflater;
	static byte[]buffer = new byte[1024] ;
	Activity activity;
	ImageLoader imageLoader;
	List<MerchantEntity> catList;
	 
	 public MerchantAdapter(Activity a,String[] text1,String[] image) 
	 {
	
		 mInflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		 activity = a;
	 
	 }
	 
	 public MerchantAdapter(Activity a, List<MerchantEntity> data)
	 {
		 catList= data;
		 activity =a;
		 mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		 imageLoader=new ImageLoader();
	 }
	 
	 public int getCount()
	 {
		 return catList.size();
	 }

	public Object getItem(int position)
	{
		 return catList.get(position);
	}

	public long getItemId(int position)
	{
		 return position;
	}
	
	 static class ViewHolder {
	 public ImageView imageView;
	 public TextView textView;
	 }
	 
	 
	 public View getView(int position, View convertView, ViewGroup parent) {

	 ViewHolder holder;
	 MerchantEntity amerchant = catList.get(position);
	 View rowView = convertView;
	 if (rowView == null) {
	 rowView = mInflater.inflate(R.layout.mercview, null, true);
	 holder = new ViewHolder();
	 holder.textView = (TextView) rowView.findViewById(R.id.TextView01);
	 holder.imageView = (ImageView) rowView.findViewById(R.id.ImageView01);
	 rowView.setTag(holder);
	 } else {
	 holder = (ViewHolder) rowView.getTag();
	 }
	 
	 holder.textView.setText(amerchant.name);
	 holder.imageView.setTag(amerchant.imgUrl);
	 
//	 Log.d("Null", ((amerchant==null)? "amerchant null ":"tidak ")+((activity==null)?"activity null ":"tidak "));
	// Log.d("debug", amerchant.name + " "+amerchant.imgUrl);
	 imageLoader.DisplayImage(amerchant.imgUrl, activity, holder.imageView,amerchant.id);

	 
	 return rowView;
	 }
	 
	 
	 private Drawable LoadImageFromWebOperations(String url, String mid)
	 {
		 File cache = new File(MusikLegal.albumArtFolder,mid+".jpg");
		 InputStream is = null;
		 
		 if(!cache.exists())
		 {
				 try {
					 
					is = (InputStream) new URL(url).getContent();
					
					BufferedInputStream bis= new BufferedInputStream(is);
					BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(cache));
					
					
					while(bis.read(buffer)>=0)
					{
						bos.write(buffer);
					}
					
					bis.close();
					bos.flush();
					bos.close();
					
					return Drawable.createFromPath(cache.getPath());
					
					
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					try {
						if(is!=null)
						is.close();
						is = (InputStream) new URL(url).getContent();			  
						 return Drawable.createFromStream(is, "src name");
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					
				}
				return null;
				
				
		 }
		 else
		 {
			 return Drawable.createFromPath(cache.getPath());
		 }
		 
		
	 }
	 
	 public void clear()
	 {
		 imageLoader.clearMemory();
	 }
	
}

